package zl.osimall.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zl.osimall.common.bean.User;
import zl.osimall.common.exception.SteduException;
import zl.osimall.common.mapper.UserMapper;
import zl.osimall.common.service.UserService;
import zl.osimall.common.utils.MD5Util;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) throws SteduException {
        User user = userMapper.selectByUsername(username);
        if(user == null){
            throw new SteduException("用户不存在，无法登录");
        }
        if(user.getStatus() == 1){
            throw new SteduException("该用户已被禁用，请联系客服");
        }
        String md5Pwd = MD5Util.getMD5(password + user.getSalt());
        if (!user.getPassword().equals(md5Pwd)){
            throw new SteduException("密码错误，无法登录");
        }
        return user;
    }

    @Override
    public Boolean register(User user) throws SteduException {
        User u = userMapper.selectByUsername(user.getUsername());
        if(u != null){
            throw new SteduException("用户名重复，无法注册");
        }
        String username = user.getUsername();
        String salt = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
        String md5Pwd = MD5Util.getMD5(user.getPassword()+salt);
        user = new User();
        user.setUsername(username);
        user.setPassword(md5Pwd);
        user.setSalt(salt);
        return userMapper.insert(user) == 1;
    }

    @Override
    public User update(User user, String oldPassword) throws SteduException {
        User oldUser = userMapper.selectByUsername(user.getUsername());
        if(oldUser == null){
            throw new SteduException("用户不存在，无法修改");
        }
        String md5Pwd = MD5Util.getMD5(oldPassword + oldUser.getSalt());
        if(!md5Pwd.equals(oldUser.getPassword())){
            throw new SteduException("原密码错误");
        }
        md5Pwd = MD5Util.getMD5(user.getPassword() + oldUser.getSalt());
        user.setPassword(md5Pwd);
        if(userMapper.update(user) != 1){
            throw new SteduException("修改失败");
        }
        return user;
    }

    @Override
    public User update(User user) {
        user.setPassword(null);
        user.setPayPassword(null);
        user.setSalt(null);
        userMapper.update(user);
        return user;
    }

    @Override
    public User updatePayPassword(User user, String newPayPassword) throws SteduException {
        User oldUser = userMapper.selectByUsername(user.getUsername());
        if(oldUser == null){
            throw new SteduException("用户不存在，无法修改");
        }
        String md5Pwd = MD5Util.getMD5(user.getPayPassword() + oldUser.getSalt());
        if(!md5Pwd.equals(oldUser.getPayPassword())){
            throw new SteduException("原支付密码错误");
        }
        md5Pwd = MD5Util.getMD5(newPayPassword + oldUser.getSalt());
        user.setPayPassword(md5Pwd);
        if(userMapper.update(user) != 1){
            throw new SteduException("修改失败");
        }
        return user;
    }

    @Override
    public User selectById(Integer id) throws SteduException {
        User user = userMapper.selectById(id);
        if (user==null){
            throw new SteduException("该用户不存在");
        }
        return user;
    }

    @Override
    public User selectByUsername(String username) throws SteduException {
        User user = userMapper.selectByUsername(username);
        if (user==null){
            throw new SteduException("该用户不存在");
        }
        return user;
    }

    @Override
    public PageInfo<User> selectByLike(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectByPage(user);
        return new  PageInfo<User>(userList);
    }
}
